package org.mspring.mlog.web.admin;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.mspring.mlog.common.ContentImage;
import org.mspring.mlog.common.constants.ContentFormat;
import org.mspring.mlog.common.constants.DailyStatus;
import org.mspring.mlog.common.exception.OperateException;
import org.mspring.mlog.entity.DailyEntity;
import org.mspring.mlog.entity.UserEntity;
import org.mspring.mlog.service.DailyEntityService;
import org.mspring.mlog.support.TabItem;
import org.mspring.mlog.web.BaseAdminController;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.db.dao.sql.builder.part.condition.Operator;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.builder.part.condition.impl.SimpleCondition;
import org.mspring.nbee.db.dao.sql.pager.OrderType;
import org.mspring.nbee.db.dao.sql.pager.SqlPager;
import org.mspring.nbee.web.support.QueryParam;
import org.mspring.nbee.web.support.WebContext;
import org.mspring.nbee.web.view.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2016-04-10 21:32
 */
@Controller
@RequestMapping("/admin/daily")
@TabItem(item = "100125")
public class DailyController extends BaseAdminController {
    private DailyEntityService dailyEntityService;

    @RequestMapping("/list")
    @TabItem(item = "100125005")
    public String list(WebContext context, Model model) {
        SqlPager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(model,
                new QueryParam(DailyEntity.Fields.title, Operator.LIKE)
        );
        conditions.add(new SimpleCondition("status", Operator.EQ, DailyStatus.NORMAL));
        PageResult<DailyEntity> result = dailyEntityService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/daily/list";
    }

    @RequestMapping("/create")
    @TabItem(item = "100125010")
    public String create(WebContext context, Model model) {
        model.addAttribute("format", ContentFormat.MARKDOWN);
        model.addAttribute("sessionId", context.getRequest().getSession().getId());
        return "/admin/daily/create";
    }

    @RequestMapping("/create/do")
    public String doCreate(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        DailyEntity daily = context.fromRequest(DailyEntity.class);

        if (StringUtils.isNotBlank(daily.getImageList())) {
            List<ContentImage> images = ContentImage.fromJson(daily.getImageList());
            daily.setImageList(JSON.toJSONString(images));
        }

        daily.setCreateTime(new Date());
        daily.setCreateUser(currentUser.getId());
        daily.setCreateUserName(currentUser.getNickname());
        daily.setUpdateTime(new Date());
        daily.setUpdateUser(currentUser.getId());
        daily.setUpdateUserName(currentUser.getNickname());
        dailyEntityService.insert(daily);
        return "redirect:/admin/daily/list";
    }


    @RequestMapping("/edit")
    @TabItem(item = "100125015")
    public String edit(WebContext context, Model model) {
        Long id = context.getParamAsLong("id");
        if (id == null) {
            throw new OperateException("操作异常：请选择要编辑的日记");
        }
        DailyEntity daily = dailyEntityService.findById(id);
        model.addAttribute("daily", daily);
        return "/admin/daily/edit";
    }


    @RequestMapping("/edit/do")
    public String doEdit(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        long id = context.getRequiredLong("id");
        DailyEntity daily = dailyEntityService.findById(id);
        daily = context.fromRequest(daily);
        daily.setUpdateTime(new Date());
        daily.setUpdateUser(currentUser.getId());
        daily.setUpdateUserName(currentUser.getNickname());
        dailyEntityService.update(daily);
        return "redirect:/admin/daily/list";
    }

    @RequestMapping("/delete/do")
    @ResponseBody
    public JsonResult delete(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        boolean flag = dailyEntityService.deleteById(id);
        return JsonResult.getJsonResult(flag);
    }

    @Autowired
    public void setDailyEntityService(DailyEntityService dailyEntityService) {
        this.dailyEntityService = dailyEntityService;
    }
}
